﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Newtonsoft.Json;

namespace DIfmClient
{
    public class JSONManager
    {
        public string GetJSONData(string url)
        {
            string jsonstring = String.Empty;
            
            using (WebClient client = new WebClient())
            {
                jsonstring = client.DownloadString(url);
            }
            
            return jsonstring;
        }

        public List<Channel> RetrieveChannels(string jsonstring, bool alphabetically)
        {
            List<Channel> allchannels = new List<Channel>();
            allchannels = JsonConvert.DeserializeObject<List<Channel>>(jsonstring);

            if (alphabetically)
            {
                var tempchannelsAZ = (from c in allchannels orderby c.Name select c);
                List<Channel> allchannelsAZ = new List<Channel>();
                
                foreach (Channel c in tempchannelsAZ)
                {
                    allchannelsAZ.Add(c);
                }
                
                return allchannelsAZ;
            }
            else return allchannels;
        }
    }
}
